package com.iyao_app.im.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.iyao_app.MyApplication;
import com.iyao_app.R;
import com.iyao_app.adapter.SimpleFragmentPagerAdapter;
import com.iyao_app.entry.NcarouselBean;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.FrescoImageLoader;
import com.iyao_app.util.ResourcesUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import github.chenupt.dragtoplayout.DragTopLayout;

/**
 * 发现页面
 */

public class DiscoverActivity extends FragmentActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.iv_next)
    ImageView mIvNext;
    @BindView(R.id.rollPV)
    Banner mRollPV;

    private SimpleFragmentPagerAdapter pagerAdapter;

    private ViewPager viewPager;

    private TabLayout tabLayout;

    private DragTopLayout dragLayout;

    private List<String> images;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover);
        ButterKnife.bind(this);
        initView();
        dragLayout = (DragTopLayout) findViewById(R.id.drag_layout);

        dragLayout.setOverDrag(false);

        pagerAdapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager(), this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
        tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void initView() {

        images = new ArrayList<>();

        //发现的轮播图
        NetUtils.getInstance().getEventFound(new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        NcarouselBean ncarouselBean = model.getModel();
                        if (ncarouselBean != null && ncarouselBean.getIyaoCarouselList() != null && ncarouselBean.getIyaoCarouselList().size() > 0) {
                            List<NcarouselBean.IyaoCarouselListBean> iyaoCarouselList = ncarouselBean.getIyaoCarouselList();
                            for (NcarouselBean.IyaoCarouselListBean bean : iyaoCarouselList) {
                                String imageUrl = bean.getImageUrl();
                                url = bean.getUrlAddress();
                                images.add(imageUrl);
                            }
                            setbanner();
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, NcarouselBean.class);


        mTvTitle.setText(ResourcesUtils.getString(R.string.find));
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void setbanner() {
        mRollPV.setImageLoader(new FrescoImageLoader());
        //设置banner样式
        mRollPV.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置banner动画效果
        mRollPV.setIndicatorGravity(BannerConfig.RIGHT);
        mRollPV.isAutoPlay(true);
        mRollPV.setImages(images);
        mRollPV.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent1 = new Intent(MyApplication.getInstance(), AboutUsActivity.class);
                intent1.putExtra(Contants.TYPE, url);
                startActivity(intent1);
            }
        });
        mRollPV.start();
    }


    public void attch(boolean b) {
        dragLayout.setTouchMode(b);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRollPV.releaseBanner();
    }

}